package com.baiwei.vpnapi.controller;
import com.baiwei.vpnapi.dao.UserMapper;
import com.baiwei.vpnapi.entity.VpnUser;
import com.baiwei.vpnapi.entity.reqentity.LoginInput;
import com.baiwei.vpnapi.exception.AccountException;
import com.baiwei.vpnapi.exception.AuthException;
import com.baiwei.vpnapi.exception.BusinessEnum;
import com.baiwei.vpnapi.service.AuthService;
import com.baiwei.vpnapi.utils.ResultVoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
* @Description: 用户登录，注册，认证
* @Param:
* @return:
* @Author: Mr.Stone
* @Date: 2020/2/24
*/

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private AuthService vpnAuthService;

    @Autowired
    private UserMapper userMapper;

    @PostMapping("/login")
    public Object login(@RequestBody LoginInput input) {
        return ResultVoUtils.success(vpnAuthService.login(input));
    }

    @GetMapping("/logout")
    public Object logout() {
        return ResultVoUtils.success(vpnAuthService.logout());
    }

    @PostMapping("/smscode")
    public Object sendSmsAuthCode(@RequestBody Map<String, String> info) {
        String phone = info.get("phone");
        if(phone == null || phone.length() == 0) {
            throw new RuntimeException("手机号为空");
        }
        VpnUser qu = new VpnUser();
        qu.setUsername(phone);
        VpnUser ru = userMapper.selectOne(new QueryWrapper<>(qu));
        if(ru == null) {
            throw new AccountException(BusinessEnum.USER_LOGIN_NOT_EXIST);
        }
        return ResultVoUtils.success(vpnAuthService.sendAuthCodeSms(phone));
    }

    @PostMapping("/usercheck")
    public Object checkUserExist(@RequestBody Map<String, String> info) {
        String username = info.get("username");
        if(username == null || username.length() == 0) {
            throw new RuntimeException("username is empty");
        }
        VpnUser qu = new VpnUser();
        qu.setUsername(username);
        VpnUser ru = userMapper.selectOne(new QueryWrapper<>(qu));
        if(ru == null) {
            throw new AccountException(BusinessEnum.USER_LOGIN_NOT_EXIST);
        }
        return ResultVoUtils.success("success");
    }

    @PostMapping("/emailcode")
    public Object sendCodeToEmail(@RequestBody Map<String, String> info)
    {
        String email = info.get("email");
        if(email == null || email.length() == 0) {
            throw new AuthException(BusinessEnum.EMAIL_EMPTY);
        }
        VpnUser qu = new VpnUser();
        qu.setUsername(email);
        VpnUser ru = userMapper.selectOne(new QueryWrapper<>(qu));
        if(ru == null) {
            throw new AccountException(BusinessEnum.USER_LOGIN_NOT_EXIST);
        }
        return ResultVoUtils.success(vpnAuthService.sendAuthCodeEmail(email));
    }

    @PostMapping("/authCodeMatch")
    public Object authCodeMatch(@RequestBody Map<String, String> info) {
        String authCode = info.get("authCode");
        if(authCode == null || authCode.equals("")) {
            throw new RuntimeException("auth code empty");
        }
        String username = info.get("username");
        if(username == null || username.length() == 0) {
            throw new RuntimeException("username is null");
        }
        return ResultVoUtils.success(vpnAuthService.matchAuthCode(authCode, username));
    }

    @PostMapping("/codeauth")
    public Object codeAuth(@RequestBody Map<String, String> info) {
        String authCode = info.get("authCode");
        if(authCode == null || authCode.equals("")) {
            throw new RuntimeException("auth code empty");
        }
        String password = info.get("password");
        if(password == null || password.length() == 0) {
            throw new RuntimeException("password empty");
        }
        String username = info.get("username");
        if(username == null || username.length() == 0) {
            throw new RuntimeException("username is null");
        }
        return ResultVoUtils.success(vpnAuthService.codeAuth(username , password, authCode));
    }


}
